<template>
    <div>
        <h1>用户列表（数据来源于ES）</h1>
        <div>
            <el-table :data="tableData" stripe style="width: 100%">
                <el-table-column prop="userId" label="ID" width="100" />
                <el-table-column prop="userName" label="登录账号" width="180" />
                <el-table-column prop="nickName" label="真实姓名" width="180" />
                <el-table-column prop="userMobile" label="手机号" width="180" />
                <el-table-column prop="userEmail" label="邮箱" width="180" />
                <el-table-column prop="userAvatar" label="头像" width="180">
                    <template #default="scope">
                        <el-image
                            v-if="scope.row.userAvatar != null"
                            :src="scope.row.userAvatar"
                            style="width: 50px; height: 50px"
                        ></el-image>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="createTime"
                    label="注册时间"
                    width="180"
                />
                <el-table-column prop="userStatus" label="状态" width="180" />
                <el-table-column label="操作" />
            </el-table>
        </div>
        <div style="margin-top: 20px;">
            <el-pagination
                v-model:current-page="searchForm.pageNum"
                v-model:page-size="searchForm.pageSize"
                :page-sizes="[2, 4, 6, 8]"
                :background="true"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
            />
        </div>
    </div>
</template>

<script setup lang="ts">
import { getUserListFromEs } from "@/api/api";
import { onMounted, ref } from "vue";

const tableData = ref([]);
const total = ref(0);
const searchForm = ref({
    pageNum: 1,
    pageSize: 2,
    keyWord: null,
});

const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`)
  searchForm.value.pageSize = val;
  loadList();
}
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`)
  searchForm.value.pageNum = val;
  loadList();
}

// 分页列表函数
const loadList = () => {
    getUserListFromEs(searchForm.value).then((res) => {
        if (res.data.code == 200) {
            tableData.value = res.data.data.records;
            total.value = res.data.data.total;
        }
    });
};

// 页面加载钩子函数
onMounted(() => {
    loadList();
});
</script>

<style scoped></style>
